<?php
namespace Home\Controller;
use Think\Controller;
class ArticleController extends Controller {
     public function index (){
         $this->display();
     }
    public function articlelist(){
       $m=M("article");
       $count= $m->count();
       $Page= new \Think\Page($count,3);
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','共%TOTAL_ROW%条%TOTAL_PAGE%页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page -> setConfig('link','indexpagenumb');//pagenumb 会替换成页码
        $Page -> setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
       $show= $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
       $list = $m->limit($Page->firstRow.','.$Page->listRows)->getField('id,title,datetime,provider');
       $this->assign('list',$list);// 赋值数据集
       $this->assign('page',$show);// 赋值分页输出
       $this->display(); // 输出模板
    }
    public function add(){
      $this->display();
    }
    public function insert(){
    if(IS_POST){   
        $_POST["datetime"]=date("Y-m-d h:i:s");
        $m=D("article");
        $m->create();       
        $m->add();
        $this->success();   
       }  else {
           $this->error('非法请求');
       }
    }
    public function del(){
             if(isset($_GET["clear"]))
             {
                    $m=D("article");
                    if($m->count())
                    {
                    $m->execute("delete from article") ;
                    }
                    $this->success('操作完成',ADMIN_URL.'/Article/del',3);
             }
             else
                 if(isset($_GET["id"])){
                      $m=M("article");
                     $key["no"]=$_GET[id];
                      $m->where($key)->delete();
                 }
                 else
             {
                $m=M("article");
                $count= $m->count();
                $Page= new \Think\Page($count,10);
                $Page -> setConfig('first','首页');
                $Page -> setConfig('last','共%TOTAL_ROW%条%TOTAL_PAGE%页');
                $Page -> setConfig('prev','上一页');
                $Page -> setConfig('next','下一页');
                $Page -> setConfig('link','indexpagenumb');//pagenumb 会替换成页码
                $Page -> setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER% ');
                $show= $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
                $list = $m->limit($Page->firstRow.','.$Page->listRows)->getField('id,title,datetime,provider');
                $this->assign('list',$list);// 赋值数据集
                $this->assign('page',$show);// 赋值分页输出
                $this->display(); // 输出模板
             }
    }
    public function modify(){
        if(IS_POST){
        $_POST["datetime"]=date("Y-m-d h:i:s");
        $key["id"]=$_GET[id];
        $m=D("article");
        $m->where($key)->save($_POST);
    $this->success("文章发表成功！");   
   }  else {
        $key["id"]=$_GET[id];
        $m=D("article");
        $data=$m->where($key)->find();
        $this->assign("vo", $data );
       $this->display();
   }
    }
    
    public function article($id)
    {
        $m=D("article");
        $key["id"]=$id;
        $list = $m->where($key)->find();
        if($list){
           $this->assign('list',$list);
        $this->display(); 
        }
        else {
            $this->error("没有该文章",URL."index.php/article/articlelist");
        } 
    }
     public function ajax()
    {
        $m=D("article");
        $list = $m->field("id,title,datetime,provider,type")->select();
        $this->ajaxReturn($list);
    }
}